package tests;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.Before;
import org.junit.Test;

import urbanparks.UserSet;
import urbanparks.Volunteer;

/**
 * Test UserSet class for errors in 
 * parsing/reading/writing.
 * 
 * @author Andrew Boguk
 * @version 1.0
 */
public class UserSetTest 
{
  /** UserSet to test. */
  private UserSet us;
  
  
  /**
   * Setup the UserSet class by creating it.
   */
  @Before
  public void setup()
  {
    us = new UserSet(new File("data/test/test_user.csv"));
  }
  
  /**
   * Test CSV parsing, and handling of odd
   * naming or parameter lengths.
   * 
   * Tests that parameter parsing works, retrieving the
   * specified user and mapping is correct.
   * 
   * Checks if blackball parsing works.
   */
  @Test
  public void loadFromFileTest() 
  {
    us.loadFromFile();
    
    //Check retrieving user correctly.
    assertEquals(us.getUser("john_smith@gmail.com").getEmailAddress(), "john_smith@gmail.com");
    assertEquals(us.getUser("val_o_ryan@gmail.com").getEmailAddress(), "val_o_ryan@gmail.com");
    assertEquals(us.getUser("bob_case@gmail.com").getEmailAddress(), "bob_case@gmail.com");
    
    //Check parameter.
    assertEquals(us.getUser("john_smith@gmail.com").getFirstName(), "John");
    assertEquals(us.getUser("val_o_ryan@gmail.com").getFirstName(), "Val");
    assertEquals(us.getUser("bob_case@gmail.com").getFirstName(), "Bob");
    
    //Check permissions.
    assertEquals(us.getUser("john_smith@gmail.com").getPermissionLevel(), 0);
    assertEquals(us.getUser("val_o_ryan@gmail.com").getPermissionLevel(), 1);
    assertEquals(us.getUser("bob_case@gmail.com").getPermissionLevel(), 2);
    
    //Check blackball working and getBlackball.
    Volunteer v = (Volunteer) us.getUser("val_o_ryan@gmail.com");
    assertEquals(v.isBlackballed(), true);   
    assertEquals(us.getBlackballedVolunteers().get(0), v);
  }
  
  /**
   * Tests integrity of writing to file by testing
   * if the first load passes, then re-writes the data
   * into the file, reloads the file, and tests to see
   * if the data is correct.
   */
  @Test
  public void writeFileTest() {
    loadFromFileTest();
    us.writeFile();
    
    loadFromFileTest();
  }
}